package com.horoscope.astrology.zodiac.signs.free.utils.ad

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import com.facebook.ads.AdIconView
import com.facebook.ads.AdOptionsView
import com.facebook.ads.MediaView
import com.facebook.ads.NativeAdLayout
import com.google.android.gms.ads.VideoController
import com.google.android.gms.ads.formats.UnifiedNativeAd
import com.google.android.gms.ads.formats.UnifiedNativeAdView
import com.horoscope.astrology.zodiac.signs.free.MmoApplication
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.utils.EventType
import com.horoscope.astrology.zodiac.signs.free.utils.FlurryEventUtil

/**
 *  作者    1024
 *  时间    2020/4/2 下午2:53
 *  文件    constellation-android
 *  描述    通过传入的AdInfo来给view添加相关资源，此部分为Facebook官方代码
 */
object NativeAdUtil {

    val context = MmoApplication.getInstance()
    lateinit var nativeAdLayout: NativeAdLayout
    lateinit var adView: LinearLayout

    private const val CUSTOM_MEDIUM = "Custom_Medium"
    private const val CUSTOM_SMALL = "Custom_Small"
    private const val HOROSCOPE_MEDIUM = "Horoscope_Medium"
    private const val HOROSCOPE_SMALL = "Horoscope_Small"
    private const val ASTROLOGY_MEDIUM = "Astrology_Medium"
    private const val ASTROLOGY_SMALL = "Astrology_Small"

    fun inflateFacebookAd(
        nativeAd: com.facebook.ads.NativeAd?,
        itemView: View,
        type: String,
        placement: String
    ) {
        if (nativeAd == null) return

        nativeAd.unregisterView()

        // Add the Ad view into the ad container.
        nativeAdLayout = itemView.findViewById(R.id.native_ad_container)
        val inflater = LayoutInflater.from(context)

        // Inflate the Ad view.  The layout referenced should be the one you created in the last step.
        val layoutId = when (type) {
            CUSTOM_MEDIUM -> R.layout.native_ad_layout_facebook
            HOROSCOPE_SMALL -> R.layout.native_ad_layout_horoscope
            ASTROLOGY_SMALL -> R.layout.native_ad_layout_astrology
            else -> R.layout.native_ad_layout_facebook
        }
        adView = inflater.inflate(layoutId, nativeAdLayout, false) as LinearLayout
        nativeAdLayout.removeAllViews()
        nativeAdLayout.addView(adView)

        // Add the AdOptionsView
        val adChoicesContainer: LinearLayout = adView.findViewById(R.id.ad_choices_container)
        val adOptionsView =
            AdOptionsView(context, nativeAd, nativeAdLayout)
        adChoicesContainer.removeAllViews()
        adChoicesContainer.addView(adOptionsView, 0)

        // Create native UI using the ad metadata.
        val nativeAdIcon: AdIconView = adView.findViewById(R.id.native_ad_icon)
        val nativeAdTitle = adView.findViewById<TextView>(R.id.native_ad_title)
        val nativeAdMedia: MediaView = adView.findViewById(R.id.native_ad_media)
        val nativeAdSocialContext =
            adView.findViewById<TextView>(R.id.native_ad_social_context)
        val nativeAdBody = adView.findViewById<TextView>(R.id.native_ad_body)
        val sponsoredLabel = adView.findViewById<TextView>(R.id.native_ad_sponsored_label)
        val nativeAdCallToAction =
            adView.findViewById<Button>(R.id.native_ad_call_to_action)

        // Set the Text.
        nativeAdTitle.text = nativeAd.advertiserName
        nativeAdBody.text = nativeAd.adBodyText
        nativeAdSocialContext.text = nativeAd.adSocialContext
        nativeAdCallToAction.visibility =
            if (nativeAd.hasCallToAction()) View.VISIBLE else View.INVISIBLE
        nativeAdCallToAction.text = nativeAd.adCallToAction
        sponsoredLabel.text = nativeAd.sponsoredTranslation

        // Create a list of clickable views
        val clickableViews: MutableList<View> = ArrayList()
        clickableViews.add(nativeAdTitle)
        clickableViews.add(nativeAdCallToAction)

        // Register the Title and CTA button to listen for clicks.
        if (type == CUSTOM_MEDIUM) {
            nativeAd.registerViewForInteraction(
                adView,
                nativeAdMedia,
                nativeAdIcon,
                clickableViews
            )
        } else {
            nativeAd.registerViewForInteraction(
                adView,
                nativeAdIcon,
                clickableViews
            )
        }
        if (placement.isNotEmpty() && placement != "Supplement") {
            FlurryEventUtil.putDataToEvent(
                EventType.TYPE_AD_DISPLAY, "type", placement
            )
            Log.e("wsTest", "FB显示了")
        }
    }

    fun inflateAdMobAd(nativeAd: UnifiedNativeAd, itemView: View, type: String) {
        var adView: UnifiedNativeAdView? = null
        val layoutInflater = LayoutInflater.from(context)
        when (type) {
            CUSTOM_MEDIUM -> adView =
                layoutInflater.inflate(R.layout.ad_custom_medium_view, null) as UnifiedNativeAdView
            CUSTOM_SMALL -> adView =
                layoutInflater.inflate(R.layout.ad_custom_small_view, null) as UnifiedNativeAdView
            HOROSCOPE_SMALL -> adView =
                layoutInflater.inflate(
                    R.layout.ad_horoscope_small_view,
                    null
                ) as UnifiedNativeAdView
            ASTROLOGY_SMALL -> adView =
                layoutInflater.inflate(
                    R.layout.ad_astrology_small_view,
                    null
                ) as UnifiedNativeAdView
        }
        val adFrame: FrameLayout = itemView.findViewById(R.id.ad_frame)
        adFrame.removeAllViews()
        adFrame.addView(adView)
        // Set the media view.
        adView!!.mediaView =
            adView.findViewById<com.google.android.gms.ads.formats.MediaView>(R.id.ad_media)

        // Set other ad assets.
        adView.headlineView = adView.findViewById(R.id.ad_headline)
        adView.bodyView = adView.findViewById(R.id.ad_body)
        adView.callToActionView = adView.findViewById(R.id.ad_call_to_action)
        adView.iconView = adView.findViewById(R.id.ad_app_icon)
        adView.priceView = adView.findViewById(R.id.ad_price)
        adView.starRatingView = adView.findViewById(R.id.ad_stars)
        adView.storeView = adView.findViewById(R.id.ad_store)
        adView.advertiserView = adView.findViewById(R.id.ad_advertiser)

        // The headline and media content are guaranteed to be in every UnifiedNativeAd.
        (adView.headlineView as TextView).text = nativeAd.headline
        adView.mediaView.setMediaContent(nativeAd.mediaContent)

        // These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
        // check before trying to display them.
        if (nativeAd.body == null) {
            adView.bodyView.visibility = View.INVISIBLE
        } else {
            adView.bodyView.visibility = View.VISIBLE
            (adView.bodyView as TextView).text = nativeAd.body
        }

        if (nativeAd.callToAction == null) {
            adView.callToActionView.visibility = View.INVISIBLE
        } else {
            adView.callToActionView.visibility = View.VISIBLE
            (adView.callToActionView as Button).text = nativeAd.callToAction
        }

        if (nativeAd.icon == null) {
            (adView.iconView as ImageView).setImageResource(
                R.mipmap.icon_google_play
            )
            adView.iconView.visibility = View.VISIBLE
        } else {
            (adView.iconView as ImageView).setImageDrawable(
                nativeAd.icon.drawable
            )
            adView.iconView.visibility = View.VISIBLE
        }

        if (nativeAd.price == null) {
            adView.priceView.visibility = View.INVISIBLE
        } else {
            adView.priceView.visibility = View.VISIBLE
            (adView.priceView as TextView).text = nativeAd.price
        }

        if (nativeAd.store == null) {
            adView.storeView.visibility = View.INVISIBLE
        } else {
            adView.storeView.visibility = View.VISIBLE
            (adView.storeView as TextView).text = nativeAd.store
        }

        if (nativeAd.starRating == null) {
            adView.starRatingView.visibility = View.INVISIBLE
        } else {
            (adView.starRatingView as RatingBar).rating = nativeAd.starRating!!.toFloat()
            adView.starRatingView.visibility = View.VISIBLE
        }

        if (nativeAd.advertiser == null) {
            adView.advertiserView.visibility = View.INVISIBLE
        } else {
            (adView.advertiserView as TextView).text = nativeAd.advertiser
            adView.advertiserView.visibility = View.VISIBLE
        }


        //此方法告诉Google Mobile Ads SDK您已完成填充
        //带有此原生广告的原生广告视图。
        adView.setNativeAd(nativeAd)

        // Get the video controller for the ad. One will always be provided, even if the ad doesn't
        // have a video asset.
        val vc = nativeAd.videoController

        // Updates the UI to say whether or not this ad has a video asset.
        if (vc.hasVideoContent()) {
            // Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The
            // VideoController will call methods on this object when events occur in the video
            // lifecycle.
            vc.videoLifecycleCallbacks = object : VideoController.VideoLifecycleCallbacks() {
                override fun onVideoEnd() {
                    // Publishers should allow native ads to complete video playback before
                    // refreshing or replacing them with another ad in the same UI location.
                    super.onVideoEnd()
                }
            }
        }
    }
}